use "C:\Users\morrisl\Dropbox\Tolerance\GSS cumulative through 2018.dta", clear

*CREATE TOLERANCE SCALES
foreach var of varlist col*{
gen `var'_1=1 if `var'==4
replace `var'_1=0 if `var'==5
}
replace colcom_1=1-colcom_1

foreach var of varlist lib*{
gen `var'_1=`var'-1
}

foreach var of varlist spk*{
gen `var'_1=2-`var'
}

foreach v in com ath mil rac homo{
egen tol`v'=rmean(col`v'_1 lib`v'_1 spk`v'_1)
}


egen tolleft=rmean(tolcom tolath)

egen colleft=rmean(colcom_1 colath_1)
egen libleft=rmean(libcom_1 libath_1)
egen spkleft=rmean(spkcom_1 spkath_1)

*OTHER COVARIATES
gen collgrad=1 if degree==4|degree==5
replace collgrad=0 if degree<=3

gen ideo3=1 if polviews<=3
replace ideo3=2 if polviews==4
replace ideo3=3 if polviews>=5 & polviews<=7

gen party=partyid
replace party=. if partyid>6

foreach i of numlist 1/4{
gen racdif`i'_1=2-racdif`i'
}
replace racdif2_1=1-racdif2_1
replace racdif4_1=1-racdif4_1

egen racdif=rmean(racdif1_1 racdif2_1 racdif3_1 racdif4_1)

gen straffil=(reliten-1)/3
gen chattend=(8-attend)/8
gen fundamentalist=(fund-1)/2

egen cityspend=rmean(natcity natcityy natcityz)
replace cityspend=(3-cityspend)/2

egen edspend=rmean(nateduc nateducy nateducz)
replace edspend=(3-edspend)/2

egen healthspend=rmean(natheal nathealy nathealz)
replace healthspend=(3-healthspend)/2

egen envirspend=rmean(natenvir natenviy natenviz)
replace envirspend=(3-envirspend)/2

egen gayok=rmean(homosex homosex1)
replace gayok=(gayok-1)/3
replace gayok=. if gayok<0|gayok>1

egen cheatok=rmean(xmarsex xmarsex1)
replace cheatok=(cheatok-1)/3

egen premarok=rmean(premarsx premars1)
replace premarok=(premarok-1)/3

egen armyspend=rmean(natarms natarmsy natarmsz)
replace armyspend=(armyspend-1)/2

gen armyconf=(conarmy-1)/2

egen genlib=rmean(straffil chattend fundamentalist cityspend edspend healthspend envirspend gayok cheatok premarok armyspend armyconf) 


gen cohort5=1910 if cohort>1905 & cohort<=1910
foreach i of numlist 1915 1920 1925 1930 1935 1940 1945 1950 1955 1960 1965 1970 1975 1980 1985 1990 1995 2000{
replace cohort5=`i' if cohort>`i'-5 & cohort<=`i'
}

gen generation=1 if cohort<1928
replace generation=2 if cohort>=1928 & cohort<=1945
replace generation=3 if cohort>=1946 & cohort<=1964
replace generation=4 if cohort>1964 & cohort<=1980
replace generation=5 if cohort>1980 & cohort!=.


*FIGURE 2
foreach var of varlist tolleft tolmil tolrac{
qbys year: asgen mean`var'=`var', weight(wtssall)
}

twoway(line meantolleft year) (line meantolmil year) (line meantolrac year)

*ONLINE APPENDIX FIGURE A1
foreach var of varlist spkleft spkmil_1 spkrac_1{
qbys year: asgen mean`var'=`var', weight(wtssall)
}

twoway(line meanspkleft year) (line meanspkmil_1 year) (line meanspkrac_1 year)

foreach var of varlist colleft colmil_1 colrac_1{
qbys year: asgen mean`var'=`var', weight(wtssall)
}

twoway(line meancolleft year) (line meancolmil_1 year) (line meancolrac_1 year)

foreach var of varlist libleft libmil_1 librac_1{
qbys year: asgen mean`var'=`var', weight(wtssall)
}

twoway(line meanlibleft year) (line meanlibmil_1 year) (line meanlibrac_1 year)



*FIGURE 3

reg tolrac i.year i.cohort [aweight=wtssall]
margins, at(year=(1976 1977 1980 1982 1984 1985 1987 1988 1989 1990 1991 1993 1994 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolrac_year")
margins, at(cohort=(1900(1)2000)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolrac_cohort")

reg tolleft i.year i.cohort [aweight=wtssall]
margins, at(year=(1976 1977 1980 1982 1984 1985 1987 1988 1989 1990 1991 1993 1994 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolleft_year")
margins, at(cohort=(1900(1)2000)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolleft_cohort")

reg tolmil i.year i.cohort [aweight=wtssall]
margins, at(year=(1976 1977 1980 1982 1984 1985 1987 1988 1989 1990 1991 1993 1994 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolmil_year")
margins, at(cohort=(1900(1)2000)) atmeans saving("C:\Users\morrisl\Dropbox\Tolerance\tolmil_cohort")

/*
*USING THE DATASETS OF MARGINS RESULTS GENERATED, RUN THE FOLLOWING TO CREATE THE FIGURE
use "C:\Users\morrisl\Dropbox\Tolerance\tolrac_year", clear
rename _margin tolracyear
merge 1:1 _at1 using "C:\Users\morrisl\Dropbox\Tolerance\tolleft_year.dta"
drop _merge
rename _margin tolleftyear
merge 1:1 _at1 using "C:\Users\morrisl\Dropbox\Tolerance\tolmil_year.dta"
drop _merge
rename _margin tolmilyear
twoway (line tolracyear _at1) (line tolleftyear _at1) (line tolmilyear _at1)

clear

use "C:\Users\morrisl\Dropbox\Tolerance\tolrac_cohort", clear
rename _margin tolraccohort
merge 1:1 _at2 using "C:\Users\morrisl\Dropbox\Tolerance\tolleft_cohort.dta"
drop _merge
rename _margin tolleftcohort
merge 1:1 _at2 using "C:\Users\morrisl\Dropbox\Tolerance\tolmil_cohort.dta"
drop _merge
rename _margin tolmilcohort
twoway (line tolraccohort _at2) (line tolleftcohort _at2) (line tolmilcohort _at2)

*/

*FIGURES 4 & 5

reg tolrac (i.collgrad i.ideo3 i.partyid i.race i.sex)##i.cohort5 [aweight=wtssall]
margins, at(cohort5=(1910(5)1995) collgrad=(0 1))
marginsplot, recastci(rline) recast(line)
margins, at(cohort5=(1910(5)1995) ideo3=(1 3))
marginsplot, recastci(rline) recast(line)
graph save "C:\Users\morrisl\Dropbox\Tolerance\updated racist education.gph"

reg tolleft (i.collgrad i.ideo3 i.partyid i.race i.sex)##i.cohort5 [aweight=wtssall]
margins, at(cohort5=(1910(5)1995) collgrad=(0 1))
marginsplot, recastci(rline) recast(line)
margins, at(cohort5=(1910(5)1995) ideo3=(1 3))
marginsplot, recastci(rline) recast(line)

reg tolmil (i.collgrad i.ideo3 i.partyid i.race i.sex)##i.cohort5 [aweight=wtssall]
margins, at(cohort5=(1910(5)1995) collgrad=(0 1))
marginsplot, recastci(rline) recast(line)
margins, at(cohort5=(1910(5)1995) ideo3=(1 3))
marginsplot, recastci(rline) recast(line)


*ONLINE APPENDIX FIGURE A2
reg tolrac (i.collgrad i.ideo3 i.partyid i.race i.sex)##i.cohort5 [aweight=wtssall]
margins, at(cohort5=(1930(5)1995) race=(1 2))
marginsplot, recastci(rline) recast(line)
margins, at(cohort5=(1930(5)1995) sex=(1 2))
marginsplot, recastci(rline) recast(line)


*FIGURE 6
qbys generation: asgen meangenlib=genlib, weight(wtssall)
qbys generation: asgen meanracdif=racdif, weight(wtssall)
reg tolrac (c.racdif c.genlib i.polviews i.partyid i.sex i.degree i.race)##i.generation [aweight=wtssall]
margins, dydx(genlib) at(generation=(1 2 3 4 5)) 
marginsplot, recast(line) addplot(line meangenlib generation, yaxis(2) yscale(alt axis(2)))
margins, dydx(racdif) at(generation=(1 2 3 4 5))
marginsplot, recast(line) addplot(line meanracdif generation, yaxis(2) yscale(alt axis(2)))


*TABLE 1
gen age40=1 if age<=40
replace age40=2 if age>40 & age!=.

table year [aweight=wtssall] if year==1976|year==2018, contents(mean tolrac mean tolmil mean tolleft)
table year age40 [aweight=wtssall] if year==1976|year==2018, contents(mean tolrac mean tolmil mean tolleft)
table year ideo3 [aweight=wtssall] if year==1976|year==2018, contents(mean tolrac mean tolmil mean tolleft)
table year collgrad [aweight=wtssall] if year==1976|year==2018, contents(mean tolrac mean tolmil mean tolleft)
